package ru.spb.osll.myfit.client.widget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;
import org.skrat.gwt.client.ui.ColorBox;
import ru.spb.osll.myfit.common.chart.Chart;
import ru.spb.osll.myfit.common.chart.ChartCurveInfo;
import ru.spb.osll.myfit.common.chart.ChartInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * User: Artem Bukhonov
 * Date: 29.03.12
 * Time: 20:07
 */
public class ChartWidget extends Composite {
  private HorizontalPanel m_rootPanel = new HorizontalPanel();
  private Image m_image = new Image() {{setStyleName("chartPicture");}};
  private TextBox m_horizontalLabelBox = new TextBox();
  private TextBox m_verticalLabelBox = new TextBox();
  private TextBox m_titleLabelBox = new TextBox();
  private TextBox m_widthBox = new TextBox();
  private TextBox m_heightBox = new TextBox();
  private Button m_rebuildButton = new Button("Rebuild");
  private ChartInfo m_info;
  private List<ChartInfoChangedListener> m_listeners = new ArrayList<ChartInfoChangedListener>();
  private final VerticalPanel m_curvePanel = new VerticalPanel();

  public interface ChartInfoChangedListener {
    void changed(ChartInfo newInfo);
  }

  public ChartWidget() {
    initWidget(m_rootPanel);
    Grid controlGrid = new Grid(5, 2);

    VerticalPanel vPanel = new VerticalPanel() {{setStyleName("chartParamsPanel");}};
    m_rootPanel.add(vPanel);
    vPanel.add(controlGrid);
    controlGrid.setHTML(0, 0, "Horizontal label");
    controlGrid.setHTML(1, 0, "Vertical label");
    controlGrid.setHTML(2, 0, "Title label");
    controlGrid.setHTML(3, 0, "Width, px");
    controlGrid.setHTML(4, 0, "Height, px");
    controlGrid.setWidget(0, 1, m_horizontalLabelBox);
    controlGrid.setWidget(1, 1, m_verticalLabelBox);
    controlGrid.setWidget(2, 1, m_titleLabelBox);
    controlGrid.setWidget(3, 1, m_widthBox);
    controlGrid.setWidget(4, 1, m_heightBox);
    //controlGrid.setWidget(3, 0, m_rebuildButton);
    VerticalPanel curveOuterPanel = new VerticalPanel() {{setStyleName("chartCurvePanel");}};
    curveOuterPanel.add(new Label("Curves (title | color):"));
    curveOuterPanel.add(m_curvePanel);
    vPanel.add(curveOuterPanel);
    vPanel.add(m_rebuildButton);
    m_rebuildButton.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent clickEvent) {
        m_info.setHorizontalLabel(m_horizontalLabelBox.getText());
        m_info.setVerticalLabel(m_verticalLabelBox.getText());
        m_info.setTitle(m_titleLabelBox.getText());
        try {
          int width = Integer.parseInt(m_widthBox.getText());
          if (width > 0)
            m_info.setWidth(width);
        }
        catch (Exception ignore) {}
        try {
          int height = Integer.parseInt(m_heightBox.getText());
          if (height > 0)
            m_info.setHeight(height);
        }
        catch (Exception ignore) {}

        for (int i = 0; i < m_curvePanel.getWidgetCount(); ++i) {
          CurveWidget widget = (CurveWidget) m_curvePanel.getWidget(i);
          widget.updateCurveValues();
        }

        for (ChartInfoChangedListener listener : m_listeners) {
          listener.changed(m_info);
        }
      }
    });
    m_rootPanel.add(m_image);
  }
  
  public void setChart(Chart chart) {
    m_image.setUrl(chart.getBase64Data());
    m_info = chart.getInfo();
    m_horizontalLabelBox.setText(chart.getInfo().getHorizontalLabel());
    m_verticalLabelBox.setText(chart.getInfo().getVerticalLabel());
    m_titleLabelBox.setText(chart.getInfo().getTitle());
    m_heightBox.setText(String.valueOf(chart.getInfo().getHeight()));
    m_widthBox.setText(String.valueOf(chart.getInfo().getWidth()));
    m_curvePanel.clear();
    for (ChartCurveInfo curve : chart.getInfo().getCurves()) {
      CurveWidget curveWidget = new CurveWidget();
      curveWidget.setChartCurve(curve);
      m_curvePanel.add(curveWidget);
    }
  }

  public void addListener(ChartInfoChangedListener listener) {
    m_listeners.add(listener);
  }

  public void clearListeners() {
    m_listeners.clear();
  }

  private class CurveWidget extends Composite {
    private HorizontalPanel m_rootPanel = new HorizontalPanel();
    private TextBox m_nameBox = new TextBox();
    private ColorBox m_colorBox = new ColorBox();
    private ChartCurveInfo m_info;

    public CurveWidget() {
      initWidget(m_rootPanel);
      m_rootPanel.add(m_nameBox);
      m_rootPanel.add(m_colorBox);
      m_colorBox.setReadOnly(true);
    }

    public void setChartCurve(ChartCurveInfo info) {
      m_info = info;
      m_nameBox.setText(info.getName());
      String colorString = Integer.toHexString(info.getColorRGB());
      colorString = "#" + colorString.substring(2);
      m_colorBox.setText(colorString);

    }

    public void updateCurveValues() {
      m_info.setName(m_nameBox.getText());
      m_info.setColorRGB(Integer.decode(m_colorBox.getText()));
    }
  }

}
